﻿using System;
using System.Text.RegularExpressions;

namespace SSP.Api
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class DirectorUtils : DirectorWrapper
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DirectorUtils"/> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        public DirectorUtils(Director parent) : base(parent)
        {
        }

        /// <summary>
        /// Is_videoes the specified fn.
        /// </summary>
        /// <param name="fn">The fn.</param>
        /// <returns></returns>
        public  bool is_video(string fn)
        {
            return Regex.Match(fn, "\\.flv|\\.mov|\\.mp4|\\.m4a|\\.m4v|\\.3gp|\\.3g2", RegexOptions.IgnoreCase).Success;
        }

        /// <summary>
        /// Is_images the specified filename.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns></returns>
        public  bool is_image(string filename)
        {
            return !is_video(filename);
        }

        /// <summary>
        /// Truncates the specified string_ renamed.
        /// </summary>
        /// <param name="string_Renamed">The string_ renamed.</param>
        /// <param name="limit">The limit.</param>
        /// <param name="tail">The tail.</param>
        /// <returns></returns>
        public  string truncate(string string_Renamed, int limit, string tail)
        {
            if (string_Renamed.Length > limit)
            {
                string_Renamed = string_Renamed.Substring(0, limit) + tail;
            }
            return string_Renamed;
        }

        /// <summary>
        /// Convert_line_breakses the specified STR.
        /// </summary>
        /// <param name="str">The STR.</param>
        /// <param name="br">if set to <c>true</c> [br].</param>
        /// <returns></returns>
        public string convert_line_breaks(string str, bool br)
        {
            return br ? str.Replace(Environment.NewLine, "<br />") : str.Replace(Environment.NewLine, " ");
        }

        /// <summary>
        /// Convert_line_breakses the specified STR.
        /// </summary>
        /// <param name="str">The STR.</param>
        /// <returns></returns>
        public string convert_line_breaks(string str)
        {
            return convert_line_breaks(str, true);
        }

        /// <summary>
        /// Converts from unix timestamp.
        /// </summary>
        /// <param name="timestamp">The timestamp.</param>
        /// <returns></returns>
        public static DateTime ConvertFromUnixTimestamp(double timestamp)
        {
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return origin.AddSeconds(timestamp);
        }

        /// <summary>
        /// Converts from unix timestamp.
        /// </summary>
        /// <param name="timestamp">The timestamp.</param>
        /// <returns></returns>
        public static DateTime ConvertFromUnixTimestamp(string timestamp)
        {
            double ts;

            if (double.TryParse(timestamp, out ts))
            {
                DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                return origin.AddSeconds(ts);
            }
            return DateTime.Now;
        }

        /// <summary>
        /// Converts to unix timestamp.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        public static double ConvertToUnixTimestamp(DateTime date)
        {
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            TimeSpan diff = date - origin;
            return Math.Floor(diff.TotalSeconds);
        }

        /// <summary>
        /// A helper method that tries to mimic php boolean conversions.
        /// </summary>
        /// <param name="val">The val.</param>
        /// <returns>The value converted to a bool.</returns>
        public static bool ToBoolean(string val)
        {
            if (string.IsNullOrEmpty(val))
                return false;
            return ! val.Equals("0");
        }

    }
}